﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;

public partial class CreateAuction : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {



        Page.Form.DefaultFocus = titleBx.ClientID;
        Page.Form.DefaultButton = createBtn.UniqueID;

        if (Session["userID"] == null)
        {
            Response.Redirect("~/Login.aspx");
        }
        else
        {
            userLbl.Text = (string)Session["userID"];
            timeLbl.Text = DateTime.Now.Date.ToString();
            //idstuff
        }

        OleDbConnection conn = null;
        OleDbDataReader reader = null;

        try
        {
            conn = new OleDbConnection(
                "Provider=Microsoft.ACE.OLEDB.12.0; " +
                "Data Source=" + Server.MapPath("App_Data/BayouFarmersDB.accdb"));
            conn.Open();

            OleDbCommand cmd = new OleDbCommand("Select MAX(ID) AS LARGEST FROM Auctions", conn);
            reader = cmd.ExecuteReader();

            while (reader.Read())
            {
                int realID = ((int)reader["LARGEST"])+1;
                string realIDstring = realID.ToString();
                idLbl.Text = realIDstring;
            }
        }
        finally
        {
            if (reader != null) reader.Close();
            if (conn != null) conn.Close();
        }
    }

    protected void createBtn_Click(object sender, EventArgs e)
    {
        OleDbConnection conn = null;

        try
        {
            string currentWinner = "N/A";
            string bidNo = "0";
            string closed = "no";
            string paid = "no";

            conn = new OleDbConnection(
                "Provider=Microsoft.ACE.OLEDB.12.0; " +
                "Data Source=" + Server.MapPath("App_Data/BayouFarmersDB.accdb"));

            string insertQuery;
            insertQuery = "INSERT INTO Auctions ";
            insertQuery += "Values ('" + idLbl.Text + "','" + userLbl.Text + "','" + titleBx.Text + "','" + timeLbl.Text + "'";
            insertQuery += ",'" + itemNoBx.Text + "','" + minBx.Text + "','" + instantBx.Text + "','" + endBx.Text + "'";
            insertQuery += ",'" + currentWinner + "','" + bidNo + "','" + closed + "','" + minBx.Text + "','" + paid + "')";

            OleDbCommand cmd = new OleDbCommand(insertQuery, conn);
            conn.Open();
            cmd.ExecuteNonQuery();
        }
        finally
        {
            conn.Close();
            //Display message notifying user that their bid was successful
            string ScriptString = "alert('Congratulations! Your Auction was successfully created!');";
            ScriptString += "window.location='Default.aspx';";
            System.Web.UI.Page page = HttpContext.Current.Handler as System.Web.UI.Page;
            page.ClientScript.RegisterStartupScript(typeof(System.Web.UI.Page), "ClientScript", ScriptString, true);
        }
    }
    protected void exitBtn_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Default.aspx");
    }
}